using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowHider
{
	/// <summary>
	/// Utility class for Serialization of Objects
	/// </summary>
	public class Serializer
	{
		/// <summary>
		/// Serializes an object into a binary file
		/// </summary>
		/// <param name="Object">Object to be serialized</param>
		/// <param name="FileName">File name to serialize object into</param>
		public static void Serialize(object Object, string FileName)
		{
			//Get a binary formatter
			BinaryFormatter bformatter = new BinaryFormatter();

			//Open a file stream to a new serialization file
			Stream stream = File.Open(FileName, FileMode.Create);
            
			//Serialize the given object into the file
			bformatter.Serialize(stream, Object);

			//Close the file stream
			stream.Close();
		}
		/// <summary>
		/// Deserialize an object from a file
		/// </summary>
		/// <param name="FileName">File name to deserialize object from</param>
		/// <returns>Deserialized Object (may require casting to desired type)</returns>
		public static object Deserialize(string FileName)
		{
			//Get a binary formatter
			BinaryFormatter bformatter = new BinaryFormatter();

			//Open a file stream to the serialization file
			Stream stream = File.Open(FileName, FileMode.Open);

			//Deserialize the object from the file
			object dObject = bformatter.Deserialize(stream);
			
			//Close the file stream
			stream.Close();

			//return the deserialized object
			return dObject;
		}
	}
}
